package com.xiaoye.qbproject.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoye.qbproject.domain.SubjectBlbc;
import com.xiaoye.qbproject.mapper.SubjectBlbcMapper;
import com.xiaoye.qbproject.service.SubjectBlbcService;
import com.xiaoye.qbproject.vo.SubjectBlbcVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【SUBJECT_BLBC(专题与兵力编程关系表)】的数据库操作Service实现
 * @createDate 2025-02-11 18:01:46
 */
@Service
public class SubjectBlbcServiceImpl extends ServiceImpl<SubjectBlbcMapper, SubjectBlbc>
        implements SubjectBlbcService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SubjectBlbcVo subjectBlbcVo) {
        List<String> oldBlbcIds = baseMapper.selectList(
                Wrappers
                        .lambdaQuery(SubjectBlbc.class)
                        .eq(SubjectBlbc::getSubjectId, subjectBlbcVo.getSubjectId())
        ).stream().map(SubjectBlbc::getBlbcId).collect(Collectors.toList());
        // 新增的编程id
        List<String> newBlbcIds = subjectBlbcVo.getBlbcIds().stream().filter(e -> !oldBlbcIds.contains(e)).collect(Collectors.toList());
        newBlbcIds.forEach(blbcId -> {
            SubjectBlbc subjectBlbc = new SubjectBlbc();
            subjectBlbc.setSubjectId(subjectBlbcVo.getSubjectId());
            subjectBlbc.setBlbcId(blbcId);
            save(subjectBlbc);
        });
    }

    /**
     * @param subjectBlbcVo
     * @description: 删除专题与兵力编程关系
     * @author: ChenYao
     * @return: void
     * @ceateDate: 2025/2/13 上午11:40
     */
    @Override
    public void delSubjectBlbc(SubjectBlbcVo subjectBlbcVo) {
        baseMapper.delete(Wrappers.lambdaQuery(SubjectBlbc.class)
                .eq(SubjectBlbc::getSubjectId, subjectBlbcVo.getSubjectId())
                .in(SubjectBlbc::getBlbcId, subjectBlbcVo.getBlbcIds()));
    }

    @Override
    public List<SubjectBlbcVo> getList(SubjectBlbcVo subjectBlbcVo) {
       List<SubjectBlbcVo>  list = baseMapper.getList(subjectBlbcVo);
        return list;
    }
}




